
$(function(){
	
	var methods = {
		detail: function(resId, resType) {
			window.open($('base').attr('href')+'pages/resource/resource.html?resourceId='+resId);
//			if(resType==1){
//				get('resource/downCount/'+resId, function(data){
//        			if(!data.success){
//        				spop(data.result, 'error');
//        			}
//        		});
//			}
		},
		uriType: function(pathType){
			//0自己的资源，1其它网站地址如百度网盘分享地址，2电驴，3磁力，4迅雷
			if(pathType==1){
				return 'fa fa-hand-o-right like-count';
			}else {
				return 'fa fa-download like-count';
			}
		},
		down: function(resId, resType, resPath){
			if(resType==0){
				window.open($('base').attr('href')+'resource/download/'+resId);
			}else{
				window.open(resPath);
				get('resource/downCount/'+resId, function(data){
        			if(!data.success){
        				spop(data.result, 'error');
        			}
        		});
			}
		},
		downloadList: function(pageclickednumber){
			var _this = this;
			_this.offset = (pageclickednumber-1)*_this.limit;
			get('resource/getList', function(data){
				if(data.success){
					Vue.set(app1, 'success', data.success);
					Vue.set(app1, 'result', data.result);
					_this.pageNum = pageclickednumber;
				}else {
					spop(data.result, 'error');
				}
			},{
				search:'',
				limit: _this.limit,
				offset: _this.offset,
				sort: 'downloadAmount',
				isDesc: true
			});
		},
		lastList: function(pageclickednumber){
			var _this = this;
			_this.offset = (pageclickednumber-1)*_this.limit;
			get('resource/getList', function(data){
				if(data.success){
					Vue.set(app2, 'success', data.success);
					Vue.set(app2, 'result', data.result);
					_this.pageNum = pageclickednumber;
				}else {
					spop(data.result, 'error');
				}
			},{
				search:'',
				limit: _this.limit,
				offset: _this.offset,
				sort: 'createTime',
				isDesc: true
			})
		},
		searchReaource: function(pageclickednumber){
			var _this = this;
			_this.offset = (pageclickednumber-1)*_this.limit;
			get('resource/getList', function(data){
				if(data.success){
					Vue.set(app, 'success', data.success);
					Vue.set(app, 'result', data.result);
					_this.pageNum = pageclickednumber;
				}else {
					spop(data.result, 'error');
				}
			},{
				search:_this.searchKeyWord,
				limit: _this.limit,
				offset: _this.offset,
				sort: 'createTime',
				isDesc: true
			});
		}
	};
	
	//下载最多
	var app1 = new Vue({
		el: '#hotResources',
		data: {
			success: false,
			result:{},
			limit: 10,
			offset: 0,
			pageNum: 1
		},
		methods: methods,
		mounted: function(){
			console.log('mounted: ');
			this.downloadList(1);
		},
		updated: function(){
			console.log('updated: ');
			var _this= this;
			$("#pager1").pager({ pagenumber: _this.pageNum, pagecount: _this.result.pageCount, buttonClickCallback: _this.downloadList });
		}
	});
	
	//最近上传
	var app2 = new Vue({
		el: '#lastResources',
		data: {
			success: false,
			result:{},
			limit: 10,
			offset: 0,
			pageNum: 1
		},
		methods: methods,
		mounted: function(){
			console.log('mounted: ');
			this.lastList(1);
		},
		updated: function(){
			console.log('updated: ');
			var _this= this;
			$("#pager2").pager({ pagenumber: _this.pageNum, pagecount: _this.result.pageCount, buttonClickCallback: _this.lastList });
		}
	});
	
	//回车不提交表单
	$('#search-form').submit(function(){
		$('#searchButton').click();
		return false;
	});
	
	//点击查询
	$('#searchButton').click(function(){
		var searchKeyWord = $('#searchKeyWord').val();
		if(isEmpty(searchKeyWord)){
			$.message({message:'请输入您要搜索资源的关键字', type:'warning'});
			$('#searchKeyWord').focus();
			return false;
		}

		Vue.set(app, 'searchKeyWord', searchKeyWord);
		app.searchReaource(1);
		
		$('#searchResult').fadeIn(2000);
	});
	
	//搜索资源
	var app = new Vue({
		el: '#resources',
		data: {
			success: false,
			result:{},
			searchKeyWord:'',
			limit: 15,
			offset: 0,
			pageNum: 1
		},
		methods: methods,
		mounted: function(){
			console.log('mounted: ');
			this.searchReaource(1);
		},
		updated: function(){
			console.log('updated: ');
			var _this= this;
			$("#pager").pager({ pagenumber: _this.pageNum, pagecount: _this.result.pageCount, buttonClickCallback: _this.searchReaource });
		}
	});
	
	console.error('提示：这次是用AES，密码没有。');
});


